package com.example.doudou.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.domain.R;
import com.example.doudou.demos.pojo.UserLogRecord;
import com.example.doudou.demos.pojo.vo.UserLogVo;
import com.example.doudou.demos.service.UserLogRecordService;
import com.example.doudou.demos.mapper.UserLogRecordMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
* @author fangtao
* @description 针对表【user_log_record】的数据库操作Service实现
* @createDate 2025-02-14 15:46:56
*/
@Service
@AllArgsConstructor
@Slf4j
public class UserLogRecordServiceImpl extends ServiceImpl<UserLogRecordMapper, UserLogRecord>
    implements UserLogRecordService{
    private final UserLogRecordMapper userLogRecordMapper;
    @Resource(name = "getDataHandleExecutor")
    private ThreadPoolTaskExecutor executorService;
//    ExecutorService executorService = Executors.newFixedThreadPool(3);

    @Override
    public R<IPage<UserLogRecord>> getPage(UserLogVo userLogVo) {
        Page<UserLogRecord> page = new Page<>(userLogVo.getPageNumber(), userLogVo.getPageLimit());
        LambdaQueryWrapper<UserLogRecord> eq = new LambdaQueryWrapper<UserLogRecord>().eq(StringUtils.isNotBlank(userLogVo.getAction()), UserLogRecord::getAction, userLogVo.getAction());
        IPage<UserLogRecord> page1 = page(page, eq);

        return  R.ok(page1);
    }

    @Override
    public UserLogRecord selectOne(UserLogVo userLogVo) {
      return  userLogRecordMapper.getOne(userLogVo);

    }

    @Override
    public R<List<UserLogRecord>> getUserLogList() throws ExecutionException, InterruptedException {

        // 保存各个线程执行后的结果集
        List<Future<UserLogRecord>> futures = new ArrayList<>(3);
        Callable<UserLogRecord> task1 = new Callable<UserLogRecord>() {
            @Override
            public UserLogRecord call() throws Exception {
                Thread.sleep(1000);
                log.info("task1执行");
                //获取列表A
                UserLogRecord userLogRecord = userLogRecordMapper.selectById("5cde39b4d47f4f4d9f1ad399af6581cb");
                return userLogRecord;
            }
        };
        Callable<UserLogRecord> task2 = new Callable<UserLogRecord>() {
            @Override
            public UserLogRecord call() throws Exception {
                log.info("task2执行");
                //获取列表B
                UserLogRecord userLogRecord = userLogRecordMapper.selectById("79975fcceffa4d51a7eaf111424ef6d1");
                return userLogRecord;
            }
        };

        Callable<UserLogRecord> task3 = new Callable<UserLogRecord>() {
            @Override
            public UserLogRecord call() throws Exception {
                log.info("task3执行");
                //获取列表C
                UserLogRecord userLogRecord = userLogRecordMapper.selectById("92e3c6080bbc4268aef6ccd2241bbba5");
                return userLogRecord;
            }
        };
        //添加线程到队列
        futures.add(executorService.submit(task1));
        futures.add(executorService.submit(task2));
        futures.add(executorService.submit(task3));

        //主线程会阻塞在这里，直到3个子线程执行完成
        //结果集汇总
        List<UserLogRecord> res = new ArrayList<>();
        for (int i = 0; i < futures.size(); i++) {
            res.add(futures.get(i).get());
        }
//        executorService.shutdown();
        return R.ok(res);
    }
}




